/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.store.index;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.List;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;
import org.apache.rocketmq.store.MappedFile;

/**
 * 索引文件index(下文简称indexFile)，
 * 由于必须以msgId或者生产者指定的消息key作为索引key，
 * 分为三部分：文件头indexHeader，一系列槽位slots，
 * hashslot存放的内容是这个槽位下所挂的最新的index节点的序号
 * 真正的索引数据index节点
 * index节点的组成是4位消息key的hashcode+8byte该index节点所对应的消息在commitlog文件中实际
 * 物理偏移量+4byte该index节点所对应的消息的存储时间与当前这个indexfile文件第一条消息存储时间的时间差
 * +4byte当前这个hashslot下，当前这个index节点的前一个index节点的编号
 *
 * 由于indexHeader，slot，index都是固定大小，所以：
 *  公式1：第n个slot在indexFile中的起始位置是这样:40+(n-1)*4
 *  公式2： 第s个index在indexFile中的起始位置是这样:40+5000000*4+(s-1)*20
 */
public class IndexFile {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);
    //每个hashslot占4个byte大小
    private static int hashSlotSize = 4;
    //每个index节点占20个byte大小
    private static int indexSize = 20;

    private static int invalidIndex = 0;
    //当前这个index文件已填充的hashslot的数量，默认是500万个
    private final int hashSlotNum;
    //当前这个index文件包含index节点的个数
    private final int indexNum;
    private final MappedFile mappedFile;
    private final FileChannel fileChannel;
    private final MappedByteBuffer mappedByteBuffer;
    //每个index文件都含有一个文件头，也就是这个对象，占用40byte
    private final IndexHeader indexHeader;

    public IndexFile(final String fileName, final int hashSlotNum, final int indexNum,
        final long endPhyOffset, final long endTimestamp) throws IOException {
        //计算这个indexfile的文件大小
        int fileTotalSize =
            IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * hashSlotSize) + (indexNum * indexSize);
        //根据文件名与文件大小生成对应的映射文件
        this.mappedFile = new MappedFile(fileName, fileTotalSize);
        //拿到这个映射文件的文件通道对象
        this.fileChannel = this.mappedFile.getFileChannel();
        //拿到这个映射文件的堆外内存缓冲
        this.mappedByteBuffer = this.mappedFile.getMappedByteBuffer();
        this.hashSlotNum = hashSlotNum;
        this.indexNum = indexNum;

        //通过堆外内存对象返回一个堆内内存缓冲对象。这个对象指向堆外那块内存区域
        //slice()这个方法是截取的意思
        ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
        //构建这个indexfile的文件头
        this.indexHeader = new IndexHeader(byteBuffer);

        if (endPhyOffset > 0) {
            //如果最后一条消息的物理偏移量大于0，分别设置文件头的当前这个index文件的第一条消息的物理偏移量
            //与当前这个index文件的最后一条消息的物理偏移量都是这个值
            this.indexHeader.setBeginPhyOffset(endPhyOffset);
            this.indexHeader.setEndPhyOffset(endPhyOffset);
        }

        if (endTimestamp > 0) {
            //如果最后一条消息的存储时间大于0，分别设置文件头的当前这个index文件的第一条消息的存储时间
            //与当前这个index文件的最后一条消息的存储时间都是这个值
            this.indexHeader.setBeginTimestamp(endTimestamp);
            this.indexHeader.setEndTimestamp(endTimestamp);
        }
    }

    public String getFileName() {
        return this.mappedFile.getFileName();
    }

    public void load() {
        this.indexHeader.load();
    }

    public void flush() {
        long beginTime = System.currentTimeMillis();
        //如果这个mappedFile被锁住了
        if (this.mappedFile.hold()) {
            //其实就是把文件头里面的属性加载到文件头的buffer里面去
            this.indexHeader.updateByteBuffer();
            //刷新到磁盘
            this.mappedByteBuffer.force();
            //释放这个mappedFile
            this.mappedFile.release();
            log.info("flush index file elapsed time(ms) " + (System.currentTimeMillis() - beginTime));
        }
    }

    public boolean isWriteFull() {
        return this.indexHeader.getIndexCount() >= this.indexNum;
    }

    public boolean destroy(final long intervalForcibly) {
        return this.mappedFile.destroy(intervalForcibly);
    }

    public boolean putKey(final String key, final long phyOffset, final long storeTimestamp) {
        //判断当前这个索引文件头中存放的索引个数是不是小于这个索引文件规定的个数
        //主要是为了看这个index是否写满了
        if (this.indexHeader.getIndexCount() < this.indexNum) {
            //对key进行hash处理，也就是拿到它的hashcode
            int keyHash = indexKeyHashMethod(key);
            //把它的hashcode对hash槽的数量取模，得到的这个key应该放在那个hash槽上，也就是在hashtable中的位置
            int slotPos = keyHash % this.hashSlotNum;
            //计算这个key放在这个索引文件中的实际位置
            //IndexHeader.INDEX_HEADER_SIZE = 索引文件头的大小
            //slotPos * hashSlotSize = hash槽的大小乘以key在hashtable中的位置
            //absSlotPos = 消息在IndexFile中的偏移量
            int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * hashSlotSize;

            FileLock fileLock = null;

            try {

                // fileLock = this.fileChannel.lock(absSlotPos, hashSlotSize,
                // false);
                //根据absSlotPos获取到这个index文件中这个槽位里面的值，
                int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
                //如果这个值小于等于无效的invalidIndex=0或者是slotValue大于当前这个indexfile中已经存放的索引个数
                if (slotValue <= invalidIndex || slotValue > this.indexHeader.getIndexCount()) {
                    //把0赋值给slotValue
                    slotValue = invalidIndex;
                }

                //timeDiff = 当前消息在broker上存储的时间跟当前索引文件中第一个消息在broker落地的时间差
                long timeDiff = storeTimestamp - this.indexHeader.getBeginTimestamp();

                timeDiff = timeDiff / 1000;

                if (this.indexHeader.getBeginTimestamp() <= 0) {
                    timeDiff = 0;
                } else if (timeDiff > Integer.MAX_VALUE) {
                    timeDiff = Integer.MAX_VALUE;
                } else if (timeDiff < 0) {
                    timeDiff = 0;
                }

                int absIndexPos =
                    IndexHeader.INDEX_HEADER_SIZE + this.hashSlotNum * hashSlotSize
                        + this.indexHeader.getIndexCount() * indexSize;

                this.mappedByteBuffer.putInt(absIndexPos, keyHash);
                this.mappedByteBuffer.putLong(absIndexPos + 4, phyOffset);
                this.mappedByteBuffer.putInt(absIndexPos + 4 + 8, (int) timeDiff);
                //这个slotValue是指这个hashslot下目前存放的最新的index节点的编号
                this.mappedByteBuffer.putInt(absIndexPos + 4 + 8 + 4, slotValue);
                //这个地方不是很明白？？？absSlotPos这个偏移量存放的内容不是index节点的编号吗？？？
                this.mappedByteBuffer.putInt(absSlotPos, this.indexHeader.getIndexCount());

                //说明这个索引文件还没有放置过索引，所以要设置开始偏移量与时间
                if (this.indexHeader.getIndexCount() <= 1) {
                    //设置文件头中的第一条消息的偏移量
                    this.indexHeader.setBeginPhyOffset(phyOffset);
                    //设置文件头中的第一条消息的存储时间
                    this.indexHeader.setBeginTimestamp(storeTimestamp);
                }
                //把已填充的hash槽数+1
                this.indexHeader.incHashSlotCount();
                //同样索引个数+1
                this.indexHeader.incIndexCount();
                //设置最后一条消息的物理偏移量
                this.indexHeader.setEndPhyOffset(phyOffset);
                //设置最后一条消息的存储时间
                this.indexHeader.setEndTimestamp(storeTimestamp);

                return true;
            } catch (Exception e) {
                log.error("putKey exception, Key: " + key + " KeyHashCode: " + key.hashCode(), e);
            } finally {
                if (fileLock != null) {
                    try {
                        fileLock.release();
                    } catch (IOException e) {
                        log.error("Failed to release the lock", e);
                    }
                }
            }
        } else {
            log.warn("Over index file capacity: index count = " + this.indexHeader.getIndexCount()
                + "; index max num = " + this.indexNum);
        }

        return false;
    }

    public int indexKeyHashMethod(final String key) {
        int keyHash = key.hashCode();
        int keyHashPositive = Math.abs(keyHash);
        if (keyHashPositive < 0)
            keyHashPositive = 0;
        return keyHashPositive;
    }

    public long getBeginTimestamp() {
        return this.indexHeader.getBeginTimestamp();
    }

    public long getEndTimestamp() {
        return this.indexHeader.getEndTimestamp();
    }

    public long getEndPhyOffset() {
        return this.indexHeader.getEndPhyOffset();
    }

    public boolean isTimeMatched(final long begin, final long end) {
        boolean result = begin < this.indexHeader.getBeginTimestamp() && end > this.indexHeader.getEndTimestamp();
        result = result || (begin >= this.indexHeader.getBeginTimestamp() && begin <= this.indexHeader.getEndTimestamp());
        result = result || (end >= this.indexHeader.getBeginTimestamp() && end <= this.indexHeader.getEndTimestamp());
        return result;
    }

    /**
     * 这个方法的核心是根据key，begin,end这三个参数
     * 先根据key定位到这个indexfile文件存储的index节点，
     * 以这个index节点不断向前推，找到时间在begin在end之间的所有消息
     * 的offset值，把他们添加到参数phyOffsets中，同时判断phyOffsets里面元素的个数
     * 大于传进来的参数phyOffsets，则就不再向前推了
     */
    public void selectPhyOffset(final List<Long> phyOffsets, final String key, final int maxNum,
        final long begin, final long end, boolean lock) {
        if (this.mappedFile.hold()) {
            //拿到key的hashcode值
            int keyHash = indexKeyHashMethod(key);
            int slotPos = keyHash % this.hashSlotNum;
            //IndexHeader.INDEX_HEADER_SIZE = 40
            int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * hashSlotSize;

            FileLock fileLock = null;
            try {
                if (lock) {
                    // fileLock = this.fileChannel.lock(absSlotPos,
                    // hashSlotSize, true);
                }

                //拿到槽位内容的值
                int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
                // if (fileLock != null) {
                // fileLock.release();
                // fileLock = null;
                // }

                if (slotValue <= invalidIndex || slotValue > this.indexHeader.getIndexCount()
                    || this.indexHeader.getIndexCount() <= 1) {
                } else {
                    for (int nextIndexToRead = slotValue; ; ) {
                        if (phyOffsets.size() >= maxNum) {
                            break;
                        }

                        int absIndexPos =
                            IndexHeader.INDEX_HEADER_SIZE + this.hashSlotNum * hashSlotSize
                                + nextIndexToRead * indexSize;

                        int keyHashRead = this.mappedByteBuffer.getInt(absIndexPos);
                        long phyOffsetRead = this.mappedByteBuffer.getLong(absIndexPos + 4);

                        long timeDiff = (long) this.mappedByteBuffer.getInt(absIndexPos + 4 + 8);
                        int prevIndexRead = this.mappedByteBuffer.getInt(absIndexPos + 4 + 8 + 4);

                        if (timeDiff < 0) {
                            break;
                        }

                        timeDiff *= 1000L;

                        long timeRead = this.indexHeader.getBeginTimestamp() + timeDiff;
                        boolean timeMatched = (timeRead >= begin) && (timeRead <= end);

                        if (keyHash == keyHashRead && timeMatched) {
                            phyOffsets.add(phyOffsetRead);
                        }

                        if (prevIndexRead <= invalidIndex
                            || prevIndexRead > this.indexHeader.getIndexCount()
                            || prevIndexRead == nextIndexToRead || timeRead < begin) {
                            break;
                        }

                        nextIndexToRead = prevIndexRead;
                    }
                }
            } catch (Exception e) {
                log.error("selectPhyOffset exception ", e);
            } finally {
                if (fileLock != null) {
                    try {
                        fileLock.release();
                    } catch (IOException e) {
                        log.error("Failed to release the lock", e);
                    }
                }

                this.mappedFile.release();
            }
        }
    }
}
